<template>
  <div>
    <el-form hide-required-asterisk="false" ref="loginForm" :model="loginForm" :rules="loginRules"
             style="width: 30%;margin: 140px auto" label-position="top">
      <div style="text-align: center">
        <h3>Login Form</h3>
      </div>
      <el-form-item style="font-weight: bold" label="账号:" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item style="font-weight: bold" label="密码:" prop="password">
        <el-input type="password" show-password v-model="loginForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <div style="margin-top: 40px">
        <el-button plain @click="handleLogin" @keyup.enter="handleLogin" type="success" style="width: 100%">登陆
        </el-button>
      </div>
      <div style="margin-top: 20px;font-weight: bold">
        <span>account:admin&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <span>password:111111</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import {ElMessage} from "element-plus";

export default {
  name: "Login",
  data: () => ({
    loginForm: {
      username: '',
      password: ''
    },
    loginRules: {
      username: [{required: true, trigger: 'blur', message: '账号必填'}, {min: 1, max: 20, message: '账号必须为1~20位'}],
      password: [{required: true, trigger: 'blur', message: '密码必填'}]
    },
  }),
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('user/doLogin', this.loginForm).then(() => {
            this.$router.push({path: '/appcontainer'})
            ElMessage({message: '登录成功！', type: 'success', duration: 2000, center: true})
            this.loginForm = {}
          }).catch(() => {
            ElMessage({message: '账号或密码有误！', type: 'error', duration: 2000, center: true})
          })
        } else {
          alert('请输入账号和密码！')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>

